﻿<%@ WebHandler Language="C#" Class="ContainerCommissionReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 在制批次外委情况查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2023.7.3
 **/

public class ContainerCommissionReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @" 
SELECT cci.mfgordername,cci.familylistnames,cci.productname,w.workcentername,e.fullname,
       cci.workflowstepname,cci.containername,c.originalstartdate,cci.qty,c.lastactivitydate,ctm.customername,
       CASE WHEN cmd.containercommidetailid IS NOT NULL THEN '是' ELSE '否' END iscomm,
       e2.fullname outemp,cm.outdate
FROM containercurrentinfo cci
LEFT JOIN container c ON cci.containerid = c.containerid
LEFT JOIN employee e ON c.startemployeeid = e.employeeid
LEFT JOIN workcenter w ON w.workcenterid = cci.fromworkcenterid
LEFT JOIN containercommidetail cmd ON cmd.containerid = cci.containerid AND cmd.workflowstepid = cci.workflowstepid AND cmd.isuse = 0
LEFT JOIN containercommissioning cm ON cm.containercommissioningid = cmd.containercommissioningid AND cm.isuse = 0
LEFT JOIN customer ctm ON ctm.customerid = cm.outcustomerid
LEFT JOIN employee e2 ON e2.employeeid = cm.outemployeeid
WHERE cci.isinstore = 0 AND cci.containerstatus = 1 ";
        //分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //工区
        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND cci.fromworkcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        //计划员
        if (jObject["jihuayuan"] != null && jObject["jihuayuan"].ToString() != "")
        {
            sql += string.Format(" AND e.employeeid = '{0}'", jObject["jihuayuan"].ToString());
        }

        //机型
        if (jObject["productfamilyname"] != null && jObject["productfamilyname"].ToString() != "")
        {
            sql += string.Format(" AND cci.familylistnames = '{0}'", jObject["productfamilyname"].ToString());
        }

        //订单号
        if (jObject["mfgordername"] != null && jObject["mfgordername"].ToString() != "")
        {
            sql += string.Format(" AND cci.mfgordername = '{0}'", jObject["mfgordername"].ToString());
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        //流水卡号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        //开卡时间
        if (jObject["containerstartdate"] != null && jObject["containerstartdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["containerstartdate"].ToString());
            sql += string.Format(" AND c.originalstartdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND c.originalstartdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", arr[1].ToString());
        }

        //外委转出时间
        if (jObject["outdate"] != null && jObject["outdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["outdate"].ToString());
            sql += string.Format(" AND cm.outdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND cm.outdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", arr[1].ToString());
        }

        //外委中
        if (jObject["iscomm"] != null && jObject["iscomm"].ToString() != "")
        {
            if (jObject["iscomm"].ToString() == "是")
            {
                sql += " AND cmd.containercommidetailid IS NOT NULL";
            }
            else
            {
                sql += " AND cmd.containercommidetailid IS NULL";
            }
        }

        sql += @"
ORDER BY cci.mfgordername,cci.familylistnames,cci.productname,cci.workflowstepname,cci.fromworkcentername,
         e.fullname,cci.workflowstepname,cci.togetherflag,cci.containername";
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dtResult = dal.GetDataTable(sql);
        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        dtResult.Columns["mfgordername"].Caption = "订单号";
        dtResult.Columns["familylistnames"].Caption = "机型";
        dtResult.Columns["productname"].Caption = "零件图号";
        dtResult.Columns["workcentername"].Caption = "主制工区";
        dtResult.Columns["fullname"].Caption = "开卡人";
        dtResult.Columns["workflowstepname"].Caption = "当前工序";
        dtResult.Columns["containername"].Caption = "流水卡号";
        dtResult.Columns["originalstartdate"].Caption = "开卡时间";
        dtResult.Columns["qty"].Caption = "数量";
        dtResult.Columns["lastactivitydate"].Caption = "当前工序开始时间";
        dtResult.Columns["customername"].Caption = "外委厂家";
        dtResult.Columns["iscomm"].Caption = "外委中";
        dtResult.Columns["outemp"].Caption = "外委转出人";
        dtResult.Columns["outdate"].Caption = "外委转出时间";
        ExportHelper.ExportExcel(dtResult, "在制批次外委情况查询报表");
    }
}